import { ref } from 'vue'
import { UserInfoApi } from './api'
import { ElMessage } from 'element-plus'
import { ConfigClass } from './config'

export class UserInfoClass {
	config
	baseData = ref()
	public visible = ref<boolean>(false)
	public src = ref()
	public formData = ref({
		username: '',
		nickname: '',
		img: '',
		github: true,
		githuburl: '',
		gitee: true,
		giteeurl: '',
		zh: true,
		zhurl: '',
		csdn: true,
		csdnurl: ''
	})
	constructor() {
		this.config = new ConfigClass(this)
		this.getInfo()
		// UserInfoApi.privateArticle().subscribe(res => {
		// 	this.baseData.value = res
		// })
	}

	/**
	 * 获取个人信息
	 */
	public getInfo() {
		UserInfoApi.getUserInfo().subscribe((res: any) => {
			this.formData.value = { ...res }
		})
	}

	/**
	 * 上传文件
	 */
	public handleAvatarSuccess = res => {
		this.formData.value.img = res.data.filename
	}
	/**
	 * 保存信息
	 */
	public save() {
		UserInfoApi.updateItem(this.formData.value).subscribe(() => {
			ElMessage.success('保存成功')
		})
	}

	/**
	 * 文章表格
	 */
	public getTableCol() {
		return { ...this.config.tableConfig, data: this.baseData.value }
	}

	/**
	 * 封面
	 */
	public preview(img) {
		this.src.value = `http://ikun-pro-ui.fun:3000/static/${img}`
		this.visible.value = true
	}
}
